package cmd

import (
	"bytes"
	"fsd/api"
	"encoding/json"
	"fmt"
	"github.com/spf13/cobra"
	"net/http"
)

var newGameCmd = &cobra.Command{
	Use:   "newgame",
	Short: "start a new game",
	Long:  "start a new game",
	Run:   newGame,
}

func init() {
	rootCmd.AddCommand(newGameCmd)
}

func newGame(cmd *cobra.Command, args []string) {
	reqBody := &api.NewGameReq{
		GameId:  cfgData.Test.TestGameId,
		UserIds: cfgData.Test.TestUserIds,
	}
	bs, err := json.Marshal(reqBody)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	resp, err := http.Post(
		"http://"+cfgData.Api.LAddr+"/newgame",
		"application/json; charset=utf-8",
		bytes.NewBuffer(bs),
	)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	defer resp.Body.Close()
	retJson := new(api.NewGameAck)
	json.NewDecoder(resp.Body).Decode(retJson)
	fmt.Printf("%+v \n", retJson)
}
